<?php
namespace Modules\Region\Entity;

use Core\Mvc\Entity\Adapter\ConfigList;
use Phalcon\Di;

class Region extends ConfigList
{
    protected $_theme;

    public function getTheme(){
        return $this->_theme;
    }

    public function setTheme($theme){
        $this->_theme = $theme;
        return $this;
    }
    public function addForm()
    {
        return false;
    }

    public function editForm()
    {
        return false;
    }

    /**
     * @return mixed
     */
    public function getLinks()
    {
        if (!$this->_links) {
            $this->_links = [
                'addBlock' => [
                    'href' => [
                        'for' => 'adminRegionBlockAddList',
                        'theme' => $this->_theme,
                        'region' => $this->getId(),
                    ],
                    'data-target' => 'right_handle',
                    'icon' => 'info',
                    'name' => '添加区块',
                ],
                'sortBlock' => [
                    'href' => [
                        'for' => 'adminRegionBlockSort',
                        'theme' => $this->_theme,
                        'region' => $this->getId(),
                    ],
                    'data-target' => 'right_handle',
                    'icon' => 'info',
                    'name' => '区块列表',
                ],
            ];
        }
        return parent::getLinks();
    }

    /**
     * @param null|array $params
     * @return \Generator
     * @throws \Exception
     */
    public static function find(array $params = null)
    {
        if (!isset($params['theme']) || !is_string($params['theme'])) {
            throw new \Exception('没有合法的参数theme');
        }

        $di = Di::getDefault();
        $themes = $di->getShared('config')->get('themes',[]);
        if (array_search(ucfirst($params['theme']),$themes) === false) {
            throw new \Exception('主题不存在：' . $params['theme']);
        }
        $data = $di->getShared('config')->get('theme.' . $params['theme'] . '.region');

        foreach ($data as $key => $region) {
            $regionObject = $di->getShared('entityManager')->get('region');
            if (!($regionObject instanceof Region)) {
                throw new \Exception('实体类型或许不存在');
            }
            $regionObject->setId($key);
            foreach ($region as $cvk => $cvv) {
                $regionObject->{$cvk} = $cvv;
            }
            $regionObject->setTheme($params['theme']);
            $regionObject->setData($region);
            yield $regionObject;
        }
    }

    /**
     * @param array|null $params
     * @return bool
     * @throws \Exception
     */
    public static function findFirst(array $params = null)
    {
        if (!isset($params['theme']) || !isset($params['region'])) {
            throw new \Exception('没有合法的参数theme');
        }

        $di = Di::getDefault();
        $themes = $di->getShared('config')->get('themes');
        if (array_search(ucfirst($params['theme']),$themes) === false) {
            throw new \Exception('主题不存在：' . $params['theme']);
        }
        $data = $di->getShared('config')->get('theme.' . $params['theme'] . '.region');
        if (!isset($data[$params['region']])) {
            return false;
        }

        $region = $data[$params['region']];
        $regionObject = $di->getShared('entityManager')->get('region');
        if (!($regionObject instanceof Region)) {
            throw new \Exception('实体类型或许不存在');
        }
        $regionObject->setTheme($params['theme']);
        $regionObject->setId($params['region']);
        foreach ($region as $cvk => $cvv) {
            $regionObject->{$cvk} = $cvv;
        }
        $regionObject->setData($region);
        return $regionObject;
    }
}